<?php

namespace app\controller\common;

use app\BaseController;
use app\Code;
use app\dao\HonorDao;
use app\model\CareerShowcase;
use app\model\Recruitment;
use app\model\TrainingLogModel;
use think\facade\Cache;

class IndexController extends BaseController
{
    // 获取首页数据（积分榜、荣誉墙、职业风采）
    public function index()
    {
        try {
            // 获取积分榜数据，使用缓存优化
            $scoreboard = Cache::get('scoreboard');
            if (!$scoreboard) {
                $scoreboard = TrainingLogModel::field('member_id, AVG(score) as avg_score')
                    ->group('member_id')
                    ->order('avg_score', 'desc')
                    ->with(['member.user'])
                    ->paginate(10); // 使用分页查询

                // 缓存积分榜数据，缓存时间为5分钟
                Cache::set('scoreboard', $scoreboard, 300);
            }

            // 获取荣誉墙数据（这里假设你有荣誉墙的相关数据模型）
            $honors = HonorDao::getAllHonors();

            // 获取职业风采数据
            $careerShowcases = CareerShowcase::all();



            return $this->returnJson([
                'scoreboard' => $scoreboard,
                    'honors' => $honors,
                    'career_showcases' => $careerShowcases
            ]);
        } catch (\Exception $e) {
            // 处理异常
            return $this->returnJson([],Code::SERVER_ERROR, '服务器错误，请稍后再试');
        }
    }
}
